layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //文件列表
    var tableIns = table.render({
        elem: '#fileList',
        url : Feng.ctxPath+'/uploadfile/fileList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "fileListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', width:60, align:"center" },
            {field: 'from_type', title: '文件来源类型', minWidth:100, align:"center"},
            {field: 'from_id', title: '文件来源', minWidth:100, align:"center"},
            {field: 'file_suffux', title: '文件后缀', minWidth:100, align:"center"},
            {field: 'file_name', title: '文件名称', minWidth:100, align:"center"},
            {field: 'real_name', title: '文件真实名称', minWidth:100, align:"center"},
            {field: 'real_file_path', title: '文件真实路径', minWidth:100, align:"center"},
            {field: 'update_man', title: '更新时间', minWidth:100, align:"center"},
            {field: 'type', title: '类型', align:'center',minWidth:150}
        ]],done: function () {
            $("[data-field='id']").css('display','none');
        }
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("userListTable",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                username: $(".usernameQuery").val()  //搜索的关键字
            }
        })
    });

    $(".addNews_btn").click(function(){
        //  addUser();
    })
    //预览打印pdf
    $(".print_btn").click(function(){
        var checkStatus = table.checkStatus("fileListTable");//选择状态{data: Array(0), isAll: false}  {data: Array(1), isAll: false}  //{account: "test", createtime: "", email: "test@qq.com", id: "2", isDeleted: "0", …}
        var data = checkStatus.data;//选择的数据
        //判断选中的状态和选中的数量
        if(data.length != 1){
            layer.msg("请选择一条数据!");
            return false;
        }
        var fileId = data[0].id;
        window.open(Feng.ctxPath + "/static/pdf/web/viewer.html?file=" + Feng.ctxPath + "/common/file/view/" + fileId)
    })

    //生成pdf文件
    $(".createpdf_btn").click(function(){

        var msg = top.layer.msg('生成pdf中，请稍候',{icon: 16,time:false,shade:0.8});
        var param={"id":"1"};
        $.ajax({
            url:Feng.ctxPath+"/uploadfile/createPdf",
            data:param,
            type:"post",
            datatype:"json",
            success:function(data){
                if(data.success){
                    setTimeout(function(){
                        top.layer.close(msg);
                        top.layer.msg(data.message);
                    },500)
                }else {
                    setTimeout(function(){
                        top.layer.close(msg);
                        top.layer.msg(data.message);
                    },500)
                }
            },
            error:function(){
                top.layer.close(msg);
                top.layer.msg(data.message);
            }
        });
    })
    //测试打包下载
 $(".downLoadZip_btn").click(function(){

      location.href=Feng.ctxPath+"/uploadfile/downLoadZipFile";
       /* var msg = top.layer.msg('文件下载中，请稍候',{icon: 16,time:false,shade:0.8});
        var param={"id":"1"};
        $.ajax({
            url:Feng.ctxPath+"/uploadfile/downLoadZipFile",
            data:param,
            type:"post",
            datatype:"json",
            success:function(data){
                if(data.success){
                    setTimeout(function(){
                        top.layer.close(msg);
                        top.layer.msg(data.message);
                    },500)
                }else {
                    setTimeout(function(){
                        top.layer.close(msg);
                        top.layer.msg(data.message);
                    },500)
                }
            },
            error:function(){
                top.layer.close(msg);
                top.layer.msg(data.message);
            }
        });*/
    })


    //列表操作
    table.on('tool(fileList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'upload_btn'){ //上传文件
            layer.msg("功能完善中...");
        }else if(layEvent === 'print_btn'){ //打印

        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此用户？',{icon:3, title:'提示信息'},function(index){
                // $.get("删除文章接口",{
                //     newsId : data.newsId  //将需要删除的newsId作为参数传入
                // },function(data){
                tableIns.reload();
                layer.close(index);
                // })
            });
        }
    });

})
